package com.hippy.erya.modular.erya;

import cn.stylefeng.roses.kernel.model.response.ResponseData;
import com.hippy.erya.base.auth.context.LoginContextHolder;
import com.hippy.erya.base.pojo.page.LayuiPageFactory;
import com.hippy.erya.base.pojo.page.LayuiPageInfo;
import com.hippy.erya.entity.DishDO;
import com.hippy.erya.entity.RawMaterialDictionaryDO;
import com.hippy.erya.entity.RawMaterialLossDO;
import com.hippy.erya.impl.RawMaterialDictionaryServiceImpl;
import com.hippy.erya.impl.RawMaterialLossServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.websocket.server.PathParam;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/erya/rawmaterialloss")
public class RawMaterialLossController {

    @Autowired
    private RawMaterialLossServiceImpl rawMaterialLossService;

    @Autowired
    private RawMaterialDictionaryServiceImpl dictionaryService;

    @RequestMapping(value = "/rawmaterialloss")
    public String rawmaterialloss() {
        return "/erya/rawmaterialloss/rawmaterialloss_setting.html";
    }

    @RequestMapping(value = "/rawmateriallossAdd")
    public String rawmateriallossAdd() {
        return "/erya/rawmaterialloss/rawmaterialloss_add.html";
    }

    @RequestMapping(value = "/rawmateriallossEdit")
    public String rawmateriallossEdit() {
        return "/erya/rawmaterialloss/rawmaterialloss_edit.html";
    }

    @RequestMapping("/queryAll")
    @ResponseBody
    public LayuiPageInfo queryByPage(@PathParam("page") Long page, @PathParam("limit") int limit,
                                     @PathParam("materialCode") String materialCode) {
        String actualMaterialCode = null;
        if (!StringUtils.isEmpty(materialCode)) {
            Map<String, String> map = dictionaryService.nameToCode(LoginContextHolder.getContext().getUserId().toString());
            actualMaterialCode = map.get(actualMaterialCode);
            if (StringUtils.isEmpty(actualMaterialCode)) {
                return LayuiPageFactory.createNonDataInfo();
            }
        }
        return LayuiPageFactory.createPageInfo(rawMaterialLossService.queryByPage(page, limit, actualMaterialCode));
    }

    @RequestMapping("/deleteData/{id}")
    @ResponseBody
    public ResponseData deleteData(@PathVariable("id") int id) {
        RawMaterialLossDO rawMaterialLossDO = rawMaterialLossService.getById(id);
        rawMaterialLossDO.deleteData(LoginContextHolder.getContext().getUserId().toString());
        rawMaterialLossService.updateById(rawMaterialLossDO);
        return ResponseData.success();
    }

    @RequestMapping("/addData")
    @ResponseBody
    public ResponseData addData(RawMaterialLossDO rawMaterialLossDO) {
        // 重复添加校验
        if (!checkRawMaterialLossAddDO(rawMaterialLossDO)) {
            return ResponseData.error("该食材损耗配置已存在！");
        }
        String user = String.valueOf(LoginContextHolder.getContext().getUserId());
        rawMaterialLossDO.setUserCode(user);
        rawMaterialLossDO.setCreatedBy(user);
        rawMaterialLossDO.setUpdatedBy(user);
        rawMaterialLossService.save(rawMaterialLossDO);
        return ResponseData.success();
    }

    @RequestMapping("/editData")
    @ResponseBody
    public ResponseData editData(RawMaterialLossDO rawMaterialLossDO) {
        String user = String.valueOf(LoginContextHolder.getContext().getUserId());
        rawMaterialLossDO.setUpdatedBy(user);
        rawMaterialLossService.updateById(rawMaterialLossDO);
        return ResponseData.success();
    }


    @RequestMapping("/rawmateriallossDetail/{id}")
    @ResponseBody
    public ResponseData editData(@PathVariable("id") int id) {
        RawMaterialLossDO rawMaterialLossDO = rawMaterialLossService.getById(id);
        RawMaterialDictionaryDO rawMaterialDictionaryDO = rawMaterialLossService.queryNameByRawMaterialCode(rawMaterialLossDO.getMaterialCode());
        if (null != rawMaterialDictionaryDO) {
            rawMaterialLossDO.setMaterialName(rawMaterialDictionaryDO.getName());
        }
        return ResponseData.success(rawMaterialLossDO);
    }

    private boolean checkRawMaterialLossAddDO(RawMaterialLossDO rawMaterialLossDO) {
        List<RawMaterialLossDO> rawMaterialLossList = rawMaterialLossService.queryByRawMaterialCode(rawMaterialLossDO.getMaterialCode());
        if (!CollectionUtils.isEmpty(rawMaterialLossList)) {
            return false;
        }
        return true;
    }
}
